<html>
<head>
  <script src="../../lib/OpenLayers.js"></script>
  <script type="text/javascript">
    var map;
    function test_initialize(t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.OverviewMap();
        t.ok( control instanceof OpenLayers.Control.OverviewMap, "new OpenLayers.Control.OverviewMap returns object" );
        t.eq( control.displayClass,  "olControlOverviewMap", "displayClass is correct" );
    }
    function test_destroy(t) {
        t.plan(6);

        // set up

        var log_drag = [], log_click = [], control;

        map = new OpenLayers.Map('map');
        map.addLayer(new OpenLayers.Layer("layer", {isBaseLayer: true}));

        control = new OpenLayers.Control.OverviewMap();
        map.addControl(control);

        map.zoomToMaxExtent();

        control.handlers.drag.destroy = function() {
            log_drag.push({"map": !!this.map.events});
        };
        control.handlers.click.destroy = function() {
            log_click.push({"map": !!this.map.events});
        };

        // test

        control.destroy();
        t.eq(log_drag.length, 2,
             "destroy() destroys drag handler twice, expected");
        if (log_drag.length == 2) {
            t.eq(log_drag[0].map, true,
                 "destroy() destroys drag handler before ovmap is destroyed (0)");
            t.eq(log_drag[1].map, false,
                 "destroy() destroys drag handler after ovmap is destroyed (1)");
        }
        t.eq(log_click.length, 2,
             "destroy() destroys click handler twice, expected");
        if (log_click.length == 2) {
            t.eq(log_click[0].map, true,
                 "destroy() destroys click handler before ovmap is destroyed (0)");
            t.eq(log_click[1].map, false,
                 "destroy() destroys click handler after ovmap is destroyed (1)");
        }

        // tear down
        map.destroy();
    }
    function test_addControl (t) {
        t.plan( 6 );
        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.OverviewMap();
        t.ok( control instanceof OpenLayers.Control.OverviewMap, "new OpenLayers.Control.OverviewMap returns object" );
        t.ok( map instanceof OpenLayers.Map, "new OpenLayers.Map creates map" );
        map.addControl(control);
        t.ok( control.map === map, "Control.map is set to the map object" );
        t.ok( map.controls[4] === control, "map.controls contains control" );
        t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Control div zIndexed properly" );
        t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Viewport div contains control div" );

        map.destroy();
    }
    function test_control_events (t) {
        t.plan( 10 );
        var evt = {which: 1}; // control expects left-click
        map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.WMS("Test Layer", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(layer);

        control = new OpenLayers.Control.OverviewMap();
        map.addControl(control, new OpenLayers.Pixel(20,20));

        var centerLL = new OpenLayers.LonLat(-71,42); 
        map.setCenter(centerLL, 11);
        t.delay_call(1, function() {
        var overviewCenter = control.ovmap.getCenter();
        var overviewZoom = control.ovmap.getZoom();
        t.eq(overviewCenter.lon, -71, "Overviewmap center lon correct");
        t.eq(overviewCenter.lat, 42, "Overviewmap center lat correct");
        t.eq(overviewZoom, 8, "Overviewmap zoomcorrect");
        
        control.mapDivClick({'xy':new OpenLayers.Pixel(5,5)});
        }, 2, function() {
        var cent = map.getCenter();
        t.eq(cent.lon, -71.3515625, "Clicking on the Overview Map has the correct effect on map lon");
        t.eq(cent.lat, 42.17578125, "Clicking on the Overview Map has the correct effect on map lat");

        control.handlers.drag = {
            last: new OpenLayers.Pixel(5,5),
            destroy: function() {}
        };
        control.rectDrag(new OpenLayers.Pixel(15, 15));
        control.updateMapToRect();
        }, 2, function() {
        var cent = map.getCenter();
        t.eq(cent.lon, -71.2734375, "Dragging on the Overview Map has the correct effect on map lon");
        t.eq(cent.lat, 42.09765625, "Dragging on the Overview Map has the correct effect on map lat");
        
        map.setCenter(new OpenLayers.LonLat(0,0), 0);
        var overviewCenter = control.ovmap.getCenter();
        var overviewZoom = control.ovmap.getZoom();
        t.eq(overviewCenter.lon, 0, "Overviewmap center lon correct -- second zoom");
        t.eq(overviewCenter.lat, 0, "Overviewmap center lat correct -- second zoom");
        t.eq(overviewZoom, 0, "Overviewmap zoomcorrect -- second zoom");
        map.destroy();
        });
    }

  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>
